/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.businessLayer;

import mnstudent.dataacesslayer.OrderSemesterDAL;
import mnstudent.datatranferobject.OrderSemesterDTO;
import mnstudent.datatranferobject.OrderSemesterDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author trungpv
 */
public class OrderSemesterBLL {
    
    OrderSemesterDAL semesterDAL;
    
    public OrderSemesterBLL()
    {
        semesterDAL = new OrderSemesterDAL();
    }
    
    /**
     * Ham lay tat ca cac HOCKY
     *
     * @return boolean
     */
    public ResultSet getAllOrderSemester() {
      
        return semesterDAL.getAllOrderSemester();
    }
    
    public ArrayList<OrderSemesterDTO> getAllOrderSemesterToArray()
    {
        ArrayList<OrderSemesterDTO> temp = new ArrayList<>();
        ResultSet rs = getAllOrderSemester();
        try {
            while(rs.next())
            {
                OrderSemesterDTO state = new OrderSemesterDTO(rs.getNString(1), rs.getNString(2));
                temp.add(state);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }
    /**
     * Ham lay HOCKY theo ma HOCKY
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getOrderSemesterById(String _id)
    {
      
        return semesterDAL.getOrderSemesterById(_id);
    }
    
     /**
     * ham them moi HOCKY xuong database
     * @param _semester
     * @return true: success, false: fail
     */
    public boolean insertOrderSemesterToDatabase(OrderSemesterDTO _semester)
    {
       return semesterDAL.insertOrderSemesterToDatabase(_semester);
    }
    
    /**
     * Ham cap nhat sua doi thong tin HOCKY
     * @param _semester
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateOrderSemesterToDatabase(OrderSemesterDTO _semester)
    {
        return semesterDAL.updateOrderSemesterToDatabase(_semester);
    }
    
    /**
     * Ham xoa mot HOCKY ra khoi database
     * @param _semester
     * @return 
     */
     public boolean deleteOrderSemesterFromDatabase(OrderSemesterDTO _semester)
    {
            
        return semesterDAL.deleteOrderSemesterFromDatabase(_semester);
    }
     
    public boolean checkInputIsInvalid(JXTable _jtb, String _input)
    {
        for(int i = 0; i<_jtb.getRowCount(); i++)
        {
            if(_jtb.getSelectedRow() != i){

                if(_input.replaceAll("\\s+", "").toLowerCase().equals(_jtb.getValueAt(i, 1).toString().replaceAll("\\s+", "").toLowerCase())){
                    return false;
                }
            }
        }
          return true;
     }
}
